<?php
/**
 *
 * @author Rodrigo Régis Palmeira <regisbsb@gmail.com>
 *
 */
namespace Foxforms\Widget\Field;
class Radio extends \Foxforms\Widget\Field
{
	protected $option	= array();
	protected $selected;
	protected $value;
	protected $showLabel	= true;

	public static function create($name)
	{
		return new self($name);
	}

	public function setValue($value)
	{
		$this->value = $value;
		return $this;
	}

	public function setOption($options = array())
	{
		$this->option = $options;
		return $this;
	}

	public function showLabel($value = true)
	{
		$this->showLabel = $value;
		return $this;
	}

	public function getValue()
	{
		return $this->value;
	}

	public function render()
	{
		$html	= ' ';
		foreach ($this->option as $key => $value)
		{
			$html	.= "<input type='radio'";

			if (!empty($this->name))
				$html	.= " name='{$this->name}'";

				$html	.= " value='{$key}'";
				$html	.= " title='{$value}'";

			if (!empty($this->readOnly))
				$html	.= " readonly";
			if (!empty($this->disabled))
				$html	.= " disabled='{$this->disabled}'";
			if (!empty($this->id))
				$html	.= " id='{$this->id}{$key}'";
			if ($this->value == $key)
				$html	.= " checked";

			if (count($this->class))
			{
				$class = implode(' ',$this->class);
				$html.= " class='{$class}'";
			}

			if (count($this->style))
			{
				$style = implode('; ',$this->style);
				$html.= " style=\"{$style}\"";
			}

			if (count($this->event))
			{
				foreach ($this->event as $event => $action)
				{
					$action = implode(';',$action);
					$html.= " {$event}=\"{$action}\"";
				}
			}

			$html	.= " />";

			if ($this->showLabel)
				$html	.= "<label for='{$this->id}{$key}'>{$value}</label>";

		}
		return $html;
	}
}